<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\crud;

use app\adminapi\controller\AuthController;
use app\services\crud\QuestionCategoryServices;
use think\facade\App;

/**
 * 题目分类管理控制器
 * Class QuestionCategory
 * @package app\adminapi\controller\crud
 */
class QuestionCategory extends AuthController
{
    /**
     * QuestionCategory constructor.
     * @param App $app
     * @param QuestionCategoryServices $services
     */
    public function __construct(App $app, QuestionCategoryServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 获取分类列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['name', ''],
            ['status', ''],
        ]);

        $list = $this->services->getList($where);

        return $this->success($list);
    }

    /**
     * 获取树形分类列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function tree()
    {
        $tree = $this->services->getTreeList();

        return $this->success($tree);
    }

    /**
     * 创建分类
     * @return mixed
     */
    public function save()
    {
        $data = $this->request->postMore([
            ['name', ''],
            ['pid', 0],
            ['sort', 0],
            ['status', 1],
        ]);

        $this->validate($data, [
            'name|分类名称' => 'require',
        ]);

        $id = $this->services->create($data);

        return $this->success(['id' => $id], '创建成功');
    }

    /**
     * 更新分类
     * @param int $id
     * @return mixed
     */
    public function update($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        $data = $this->request->postMore([
            ['name', ''],
            ['pid', ''],
            ['sort', ''],
            ['status', ''],
        ]);

        // 过滤空值
        $data = array_filter($data, function ($value) {
            return $value !== '';
        });

        if (empty($data)) {
            return $this->fail('没有要更新的数据');
        }

        $this->services->update((int)$id, $data);

        return $this->success('更新成功');
    }

    /**
     * 删除分类
     * @param int $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }

        $this->services->delete((int)$id);

        return $this->success('删除成功');
    }
}

